#include "zoomableimagewidget.h"

#include <QWheelEvent>
#include <QMouseEvent>
#include <QPainter>
#include <QDebug>
#include <QPushButton>

ZoomableImageWidget::ZoomableImageWidget(QWidget *parent) : QWidget(parent) {
    m_zoomInterval = 0.1;
    m_zoomMaxValue = 2;
    m_zoomMinValue = 0.5;

//    initButtons();
//    setWindowFlag(Qt::FramelessWindowHint);  // 无边框
    setAttribute(Qt::WA_DeleteOnClose);     // 关闭窗口时delete
    setAttribute(Qt::WA_ShowModal, true);  // 模态显示
    setWindowFlags(Qt::WindowCloseButtonHint);
}

ZoomableImageWidget::~ZoomableImageWidget() {
}

void ZoomableImageWidget::setPixmap(const QPixmap &pixmap) {
    m_currentPixmap = pixmap;
    this->setFixedSize(m_currentPixmap.width(), m_currentPixmap.height());
//    m_btnClose->move(this->width() - m_btnClose->width() - 10, 10);
    m_zoomValue = 1;
    m_offsetX = 0;
    m_offsetY = 0;
    update();
}

#if 0
void ZoomableImageWidget::setPixmaps(const QVector<QPixmap> &pixmaps) {
    m_vecPixmaps.clear();
    m_vecPixmaps = pixmaps;
    m_pixmapsCount = pixmaps.count();
    m_currentPixmapIndex = 0;
    setPixmap(pixmaps.at(m_currentPixmapIndex));
}
#endif

void ZoomableImageWidget::setZoomInterval(float interval) {
    m_zoomInterval = interval;
}

void ZoomableImageWidget::setZoomMaxValue(float max) {
    m_zoomMaxValue = max;
}

void ZoomableImageWidget::setZoomMinValue(float min) {
    m_zoomMinValue = min;
}

void ZoomableImageWidget::initButtons() {
    QString style = QString("QPushButton {"
                            "border: none;"
                            "background-color: rgba(200, 200, 200, 0.7);"
                            "border-radius: 24px;"
                            "font-size: 24px;"
                            "color: #ffffff;"
                            "}"
                            "QPushButton:hover {"
                            "background-color: rgba(255, 0, 0, 0.7);"
                            "}");
    m_btnClose = new QPushButton(this);
    m_btnClose->setText(QString("×"));
    m_btnClose->setMinimumSize(48, 48);
    m_btnClose->setMaximumSize(48, 48);
    m_btnClose->setStyleSheet(style);
    connect(m_btnClose, &QPushButton::released, [ = ] {
        this->close();
    });

#if 0
    m_btnPrev = new QPushButton(this);
    m_btnPrev->setText(QString("<"));
    m_btnPrev->setMinimumSize(48, 48);
    m_btnPrev->setMaximumSize(48, 48);
    m_btnPrev->setStyleSheet(style);
    connect(m_btnPrev, &QPushButton::released, [ = ] {

    });

    m_btnNext = new QPushButton(this);
    m_btnNext->setText(QString(">"));
    m_btnNext->setMinimumSize(48, 48);
    m_btnNext->setMaximumSize(48, 48);
    m_btnNext->setStyleSheet(style);
    connect(m_btnNext, &QPushButton::released, [ = ] {

    });
#endif
}

void ZoomableImageWidget::zoomInImage() {
    m_zoomValue += m_zoomInterval;
    if (m_zoomValue > m_zoomMaxValue) {
        m_zoomValue = m_zoomMaxValue;
    }
}

void ZoomableImageWidget::zoomOutImage() {
    m_zoomValue -= m_zoomInterval;
    if (m_zoomValue < m_zoomMinValue) {
        m_zoomValue = m_zoomMinValue;
    }
}

void ZoomableImageWidget::paintEvent(QPaintEvent *event) {
    Q_UNUSED(event);
    QPainter painter(this);
    // 平移
    painter.translate(m_offsetX, m_offsetY);

    // 缩放
    painter.scale(m_zoomValue, m_zoomValue);

    // 绘制图像
    painter.drawPixmap(this->rect(), m_currentPixmap);
}

void ZoomableImageWidget::mousePressEvent(QMouseEvent *event) {
    setCursor(Qt::ClosedHandCursor);
    m_pressPoint = event->pos();
    m_isPressed = true;
}

void ZoomableImageWidget::mouseMoveEvent(QMouseEvent *event) {
    if (m_isPressed) {
        QPoint currentPoint = event->pos();
        int offsetX = currentPoint.x() - m_pressPoint.x();
        int offsetY = currentPoint.y() - m_pressPoint.y();

        m_offsetX += offsetX;
        m_offsetY += offsetY;

        m_pressPoint = currentPoint;
        update();
    }
}

void ZoomableImageWidget::mouseReleaseEvent(QMouseEvent *event) {
    Q_UNUSED(event);
    setCursor(Qt::ArrowCursor);
    m_isPressed = false;
}

void ZoomableImageWidget::mouseDoubleClickEvent(QMouseEvent *event) {
    Q_UNUSED(event);
    m_zoomValue = 1;
    m_offsetX = 0;
    m_offsetY = 0;
    update();
}

void ZoomableImageWidget::wheelEvent(QWheelEvent *event) {
    // 通过delta()获取到的值来判断滚轮是往前还是往后
    int value = event->delta();
    if (value > 0) {
        zoomInImage();
    } else {
        zoomOutImage();
    }
    update();
}
